package com.atserver.listener;

import cn.hutool.core.util.StrUtil;
import com.atserver.service.CameraEndTask;
import com.atserver.service.CameraStartTask;
import com.atserver.service.RobotKey;
import com.atserver.vedio.UsbCameraControl;
import com.atserver.vedio.UsbCameraControlImpl;
import com.atserver.windows.ScannerWindow;
import lombok.extern.slf4j.Slf4j;

import javax.swing.*;
import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

/**
 * @program: atserver
 * @Description: 监听扫描仪
 * @author: Mr.Cheng
 * @date: 2022/5/10 4:54 下午
 */
@Slf4j
public class TextFieldKeyListener implements KeyListener {

    private ScannerWindow scannerWindow;

    public TextFieldKeyListener(ScannerWindow scannerWindow) {
        this.scannerWindow = scannerWindow;
    }
    /**
     * Invoked when a key has been typed.
     * See the class description for {@link KeyEvent} for a definition of
     * a key typed event.
     *
     * @param e
     */
    @Override
    public void keyTyped(KeyEvent e) {
        if (e.getKeyChar() == KeyEvent.VK_ENTER) {
            log.info("Type enter");
        }
    }

    /**
     * Invoked when a key has been pressed.
     * See the class description for {@link KeyEvent} for a definition of
     * a key pressed event.
     *
     * @param e
     */
    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_ENTER) {
            log.info("Press enter");
        }
    }

    /**
     * Invoked when a key has been released.
     * See the class description for {@link KeyEvent} for a definition of
     * a key released event.
     *
     * @param e
     */
    @Override
    public void keyReleased(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_ENTER) {
            log.info("Release enter");
            SwingWorker swingWorker = new SwingWorker() {
                @Override
                protected Object doInBackground() throws Exception {
                    try {
                        Robot robot = new Robot();
                        robot.setAutoWaitForIdle(true);
                        int x = Integer.valueOf(scannerWindow.getXCoordinateTextField().getText());
                        int y = Integer.valueOf(scannerWindow.getYCoordinateTextField().getText());
                        robot.mouseMove(x, y);
                        robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
                        robot.delay(100);
                        robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
                        robot.delay(100);
                        RobotKey robotKey = new RobotKey(robot);
                        robotKey.type(scannerWindow.getInputBarCodeTextField().getText());
                        robot.delay(100);
                        robot.keyPress(KeyEvent.VK_ENTER);
                        robot.delay(100);
                        robot.keyRelease(KeyEvent.VK_ENTER);
                        robot.delay(1000);
                        robot.mouseMove(scannerWindow.getX() + 10, scannerWindow.getY() + 80);
                        robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
                        robot.delay(100);
                        robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
                    } catch (AWTException ex) {
                        log.error("Robot error", ex);
                    }
                    return null;
                }

                @Override
                protected void done() {
                    /**
                     * 清空输入框
                     */
                    scannerWindow.getInputBarCodeTextField().setText("");
                }
            };
            swingWorker.execute();
            UsbCameraControl usbCamera = UsbCameraControlImpl.getInstance();
            CameraEndTask endTask = new CameraEndTask(usbCamera);
            endTask.execute();
            String barCode = scannerWindow.getInputBarCodeTextField().getText();
            if(!StrUtil.isBlankIfStr(barCode)){
                String filename = "./data/" + barCode + ".mp4";
                CameraStartTask startTask = new CameraStartTask(usbCamera,filename);
                startTask.execute();
            }

        }
    }
}
